package com.rocksea.app.tasks;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.rocksea.app.MyApplication;
import com.rocksea.app.db.history.HistoryDbHelper;
import com.rocksea.app.db.history.HistoryTable;
import com.rocksea.app.entry.InspectionForm;
import com.rocksea.app.entry.PileInfo;
import com.rocksea.app.fragment.PilNoListFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * Created by 吴志华 on 2017/9/29.
 * 异步任务
 *
 */

public class AskTask extends AsyncTask<Void,Void,ArrayList<PileInfo>>
{
    private String planNumber;
    private ArrayList<InspectionForm.PileBean> planList;
    private int methodCode;

    public AskTask(int methodCode,String planNumber,ArrayList<InspectionForm.PileBean> planList)
    {
        this.methodCode = methodCode;
        this.planNumber = planNumber;
        this.planList = planList;
    }
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected ArrayList<PileInfo> doInBackground(Void... params)
    {
        ArrayList<HistoryTable> mPileNoData = HistoryDbHelper.getInstance(MyApplication.getContext()).queryBySerilNo(planNumber);
        ArrayList<PileInfo> pileInfos = new ArrayList<>();
        boolean isHaveReceive = false;
        for (int i = 0; i < planList.size(); i++)
        {
            InspectionForm.PileBean pile = planList.get(i);
            for (int i1 = 0; i1 < mPileNoData.size(); i1++)
            {
                if (pile.pileNo.equals(mPileNoData.get(i1).getPileNo())&&methodCode == mPileNoData.get(i1).getType())
                {
                    isHaveReceive = true;
                    PileInfo pileInfo = new PileInfo();
                    pileInfo.setPileBottomH(pile.pileBottomH);
                    pileInfo.setPileId(pile.pileId);
                    pileInfo.setPileNo(pile.pileNo);
                    pileInfo.setPileTopH(pile.pileTopH);
                    pileInfo.setDataType(mPileNoData.get(i1).getType());
                    pileInfo.setBasicInfoId(mPileNoData.get(i1).BaseInfoId);
                    pileInfo.setReceiveTime(mPileNoData.get(i1).getTestTime());
                    pileInfo.setSerilNo(planNumber);
                    pileInfo.setLoadTime(mPileNoData.get(i1).LoadTime);
                    pileInfos.add(pileInfo);
                    break;
                }
                else
                    isHaveReceive = false;
            }
            if (!isHaveReceive) //没有传到手机上的桩
            {
                PileInfo pileInfo = new PileInfo();
                pileInfo.setPileBottomH(pile.pileBottomH);
                pileInfo.setPileId(pile.pileId);
                pileInfo.setPileNo(pile.pileNo);
                pileInfo.setPileTopH(pile.pileTopH);
                pileInfo.setSerilNo(planNumber);
                pileInfo.setBasicInfoId(null);
                pileInfo.setReceiveTime(null);
                pileInfo.setLoadTime(0);
                pileInfo.setDataType(-1);
                pileInfos.add(pileInfo);
            }
        }

        return pileInfos;
    }
    @Override
    protected void onPostExecute(ArrayList<PileInfo> tables)
    {
        super.onPostExecute(tables);
        EventBus.getDefault().post(tables);
    }
}
